package com.haojin.java1;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;

/**
 * @author .29.
 * @create 2022-04-21 7:22
 */
/*
java内置的四大核心的函数式接口

消费型接口Consumer<T>       void accept(T t)
供给型接口Supplier<T>       T get()
函数型接口Function<T,R>     R apply(T t)
断定型接口Predicate<T>      boolean test(T t)
 */
public class LambdaTest2 {
    @Test
    public void test1(){
        happyTime(500, new Consumer<Double>() {
            @Override
            public void accept(Double money) {
                System.out.println("学习太累了，去天上人间花费："+money);
            }
        });

        System.out.println("********************************");

        happyTime(29, money -> System.out.println("学习太累了，去天上人间喝了口水："+money));
    }

    public void happyTime(double money, Consumer<Double> con){
        con.accept(money);
    }


    @Test
    public void test2(){

        List<String> list = Arrays.asList("天津","北京","南京","东京","西京","普京");
        List<String> filterString = FilterString(list, new Predicate<String>() {

            @Override
            public boolean test(String s) {
                return s.contains("京");
            }
        });
        System.out.println(filterString);

        System.out.println("**************************");

        List<String> filterString1 = FilterString(list,s -> s.contains("津"));
        System.out.println(filterString1);

    }
    //根据给定的规则，过滤集合中的字符串，此规则有Predicate的方法决定
    public List<String> FilterString(List<String> list, Predicate<String > pre){
        ArrayList<String> filterList = new ArrayList<>();
        for (String s :
                list) {
            if (pre.test(s)){
                filterList.add(s);
            }
        }
        return filterList;
    }
}
